<!--
* description: 交付地管理---交付地变更申请
* author:xlc
* createdDate: 2020-12-1
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="false"
      :dynamic-is-show-more-btn="true"
      :callback-obj="callbackObj"
    />
    <edit
      v-if="editPopupsVisible"
      :key="editPopupsKey"
      :popups-visible="editPopupsVisible"
      :data-for-form="editRowData"
      :popups-state="editPopupsState"
      :title-name="titleName"
      @close="close"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import { orgApis } from '@/api/vesalesapi.js'
import OneTableTemplate from '@/components/templates/oneTable'
import Edit from './edit'
import Vue from 'vue'
export default {
  name: 'DeliveryPlaceChange',
  components: {
    OneTableTemplate,
    Edit
  },
  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, 'supplierMessage-edit'], () => {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      titleName: '',
      editRowData: {},
      editPopupsState: '',
      editPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: orgApis.deliveryPlaceChangeQuery,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          name: 'reset',
          position: 'right' // 重置
        },
        {
          compKey: 'btnKey5',
          type: '',
          size: 'small',
          clickEvent: () => this.add(),
          text: '新增', // 新增
          name: 'add',
          position: 'left'
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '变更单号：',
              codeField: 'jfdCode',
              isMul: false,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '关联订单号：',
              codeField: 'associatedOrder',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '申请日期：',
              codeField: 'applicationStartTime,applicationEndTime',
              component: () => import('@/components/org/datePicker/datePicker.vue'),
              dateType: 'daterange',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '申请人：',
              codeField: 'applicationName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '状态：',
              codeField: 'jfdStatus',
              lookuptype: 'VE1320',
              component: () => import('@/components/org/LookupValue'),
              type: 'dropdownList',
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '基础车型名称：',
              codeField: 'carConfigId',
              textField: 'carConfigCn',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carConfigId,carConfigCn',
              customObj: {
                api: orgApis.listOfCarModel, selectType: '1'
              },
              isMust: false
            }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true
            },
            {
              prop: 'controlBtn',
              label: '操作', // 操作
              codeField: 'controlBtn',
              width: 100,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '编辑', // 编辑
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: index => { this.edit(index) },
                  rowFileds: 'show:lookupValueName-未申请',
                  component: () => import('@/components/org/linkButton')
                },
                {
                  compKey: 'propKey2',
                  labelName: '申请',
                  align: 'center',
                  codeField: 'editControlBtn',
                  clickEvent: index => { this.apply(index) },
                  rowFileds: 'show:lookupValueName-未申请',
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'jfdCode',
              label: '变更单号'
            },
            {
              prop: 'lookupValueName',
              label: '状态'
            },
            {
              prop: 'applicationDate',
              label: '申请时间'
            },
            {
              prop: 'applicationName',
              label: '申请人'
            },
            {
              prop: 'jfdReason',
              label: '交付地变更原因'
            },
            {
              prop: 'oldDlrShortName',
              label: '原交付门店'
            },
            {
              prop: 'oldCarHouseName',
              label: '原交付仓库'
            },
            {
              prop: 'newDlrShortName',
              label: '变更后交付门店'
            },
            {
              prop: 'newCarHouseName',
              label: '变更后交付仓库'
            },
            {
              prop: 'associatedOrder',
              label: '关联订单号'
            },
            {
              prop: 'carType',
              label: '基础车型名称'
            }
          ],
      formField: {
        jfdCode: '',
        associatedOrder: '',
        applicationStartTime: '',
        applicationEndTime: '',
        applicationName: '',
        jfdStatus: '',
        carConfigId: ''
      },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      // 单表格模版页面组件回调事件
      callbackObj: {
        // // 手动勾选时
        // onCheckSelect: (selection, row) => {
        //   // 清除所有选中
        //   this.$refs.multipleTable.$refs.multipleTable.clearSelection()
        //   if (selection.length === 0) return
        //   // 将当前点击项选中
        //   this.$refs.multipleTable.$refs.multipleTable.toggleRowSelection(row, true)
        // },
        // // 当选择项发生变化时会触发该事件（多选时）
        // onCheckSelectChange: (selection) => {
        //   if (selection.length === 1) {
        //     this.editRowData = selection[0]
        //   }
        // }
      }
    }
  },
  mounted() {
    this.queryTable(1)
  },
  methods: {
    // 申请
    apply(index) {
      this.$refs.multipleTable.listLoading = true
      const that = this
      var querydata = {
        jfdId: this.$refs.multipleTable.list[index].jfdId,
        updateControlId: this.$refs.multipleTable.list[index].updateControlId
      }
      const queryObj = {
        // api配置
        apiConfig: orgApis.deliveryPlaceChangeApply,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          // 当前中台使用的名称有dataInfo、info，具体查看API文档
          dataInfo: querydata
        }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        this.$refs.multipleTable.listLoading = false
        this.$message({
          message: response.msg,
          duration: 2000
        })
        if (response.result === '1') {
          this.queryTable(1)
        }
      })
    }
  }
}
</script>
